package pl.com.sthaether.mc.main.workspace;

import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.StackPane;

public class MCWorkspacePane extends StackPane {// TODO to decide - anchor pane/ canvas -or.... stack pane with layers of anchorpane/canvas


    private double dragBaseX, dragBaseY;
    private double dragBase2X, dragBase2Y;

    private Group currentContent;

    private MCWorkspacePane() {
        setAlignment(Pos.CENTER);


//        Rectangle r = new Rectangle(50, 50);
//        r.setFill(Color.RED);
//        Pane p = new Pane();
//        p.setStyle("-fx-background-image: url(\"http://www.bogotobogo.com/images/java/tutorial/java_images/Duke256.png\"); -fx-background-repeat: no-repeat; -fx-background-size: contain;");
//        Canvas can = new Canvas();
//        can.setOnMouseClicked(event -> can.getGraphicsContext2D().fillRect(event.getX() - 2, event.getY() - 2, 5, 5));
//        getChildren().addAll(r, p, can);
//
//        ObservableList<PieChart.Data> pieChartData =
//                FXCollections.observableArrayList(
//                        new PieChart.Data("Grapefruit", 13),
//                        new PieChart.Data("Oranges", 25),
//                        new PieChart.Data("Plums", 10),
//                        new PieChart.Data("Pears", 22),
//                        new PieChart.Data("Apples", 30));
//        final PieChart chart = new PieChart(pieChartData);
//        chart.setTitle("Imported Fruits");
//
//
//        chart.layoutXProperty().bind(widthProperty().multiply(0.1));
//        chart.layoutYProperty().bind(heightProperty().multiply(0.1));
//
//        p.getChildren().add(chart);
//
//
//        r.widthProperty().bind(prefWidthProperty());
//        r.heightProperty().bind(prefHeightProperty());
//        can.widthProperty().bind(prefWidthProperty());
//        can.heightProperty().bind(prefHeightProperty());
//        setPrefSize(0, 0);

//        Color[] colors = new Color[]{Color.YELLOW, Color.RED, Color.ORANGE, Color.VIOLET, Color.CHOCOLATE, Color.YELLOW, Color.AZURE};
//        int currentColor = 0;
//
//
//        SimpleFeatureCollection c = null;
//        FileDataStore store = null;
//        try {
//            store = new ShapefileDataStore(new URL("file", "", "/Users/kaqu/IdeaProjects/mc-mapper/[MC]Mapper/ne_50m_admin_0_countries.shp"));
//            SimpleFeatureSource featureSource = null;
//            featureSource = store.getFeatureSource();
//            c = featureSource.getFeatures();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        SimpleFeatureIterator featuresIterator = c.features();
//        Coordinate[] coords;
//        Geometry polygon;
//        Point centroid;
//        Bounds bounds;
//        while (featuresIterator.hasNext()) {
//            SimpleFeature o = featuresIterator.next();
//            String name = (String) o.getAttribute("NAME");
//            Object geometry = o.getDefaultGeometry();
//
//            if (geometry instanceof MultiPolygon) {
//                MultiPolygon multiPolygon = (MultiPolygon) geometry;
//
//                centroid = multiPolygon.getCentroid();
//                final Text text = new Text(name);
//                bounds = text.getBoundsInLocal();
//                text.getTransforms().add(new Translate(centroid.getX(), centroid.getY()));
//                text.getTransforms().add(new Scale(0.1, -0.1));
//                text.getTransforms().add(new Translate(-bounds.getWidth() / 2., bounds.getHeight() / 2.));
//                texts.getChildren().add(text);
//
//                for (int geometryI = 0; geometryI < multiPolygon.getNumGeometries(); geometryI++) {
//                    polygon = multiPolygon.getGeometryN(geometryI);
//
//                    coords = polygon.getCoordinates();
//                    Path path = new Path();
//                    path.setStrokeWidth(0.05);
//                    currentColor = (currentColor + 1) % colors.length;
//                    path.setFill(colors[currentColor]);
//                    path.getElements().add(new MoveTo(coords[0].x, coords[0].y));
//                    for (int i = 1; i < coords.length; i++) {
//                        path.getElements().add(new LineTo(coords[i].x, coords[i].y));
//                    }
//                    path.getElements().add(new LineTo(coords[0].x, coords[0].y));
//                    map1.getChildren().add(path);
//                }
//            }
//        }
//
//        map.translateXProperty().set(600);
//        map.translateYProperty().set(300);
//        map.scaleXProperty().set(3);
//        map.scaleYProperty().set(-3);
//        this.setOnMousePressed(new EventHandler<MouseEvent>() {//test
//            public void handle(MouseEvent event) {
//                map.setCursor(Cursor.MOVE);
//                dragBaseX = map.translateXProperty().get();
//                dragBaseY = map.translateYProperty().get();
//                dragBase2X = event.getSceneX();
//                dragBase2Y = event.getSceneY();
//            }
//        });
//        this.setOnMouseDragged(new EventHandler<MouseEvent>() {//test
//            public void handle(MouseEvent event) {
//                map.setTranslateX(dragBaseX + (event.getSceneX() - dragBase2X));
//                map.setTranslateY(dragBaseY + (event.getSceneY() - dragBase2Y));
//            }
//        });
//        this.setOnMouseReleased(new EventHandler<MouseEvent>() {//test
//            public void handle(MouseEvent event) {
//                map.setCursor(Cursor.DEFAULT);
//            }
//        });
//
//        map.getChildren().add(map1);
//        map.getChildren().add(texts);
//        this.getChildren().add(map);


    }

    public static MCWorkspacePane getInstance() {
        return MCHolder.INSTANCE;
    }

    public Group getCurrentContent() {
        return currentContent;
    }

    public void registerNewContent(Group group) {
        getChildren().clear();
        getChildren().add(group);

//        group.scaleXProperty().set(3);
//        group.scaleYProperty().set(-3);
        this.setOnMousePressed(event -> {
            MCWorkspacePane.this.setCursor(Cursor.OPEN_HAND);
            dragBaseX = group.translateXProperty().get();
            dragBaseY = group.translateYProperty().get();
            dragBase2X = event.getSceneX();
            dragBase2Y = event.getSceneY();
        });
        this.setOnMouseDragged(event -> {
            group.setTranslateX(dragBaseX + (event.getSceneX() - dragBase2X));
            group.setTranslateY(dragBaseY + (event.getSceneY() - dragBase2Y));

        });
        this.setOnMouseReleased((event) -> MCWorkspacePane.this.setCursor(Cursor.DEFAULT));

        // TODO - CURRENT
//        VBox vbox = new VBox();
//        final Button plus = new Button("+");
//        plus.setOnAction(new EventHandler<ActionEvent>() {
//            public void handle(ActionEvent event) {
//                zoom(1.4);
//            }
//
//            private void zoom(double d) {
//
//                group.scaleXProperty().set(group.scaleXProperty().get() * d);
//                group.scaleYProperty().set(group.scaleYProperty().get() * d);
//            }
//        });
//        vbox.getChildren().add(plus);
//        final Button minus = new Button("-");
//        minus.setOnAction(new EventHandler<ActionEvent>() {
//
//            public void handle(ActionEvent event) {
//                zoom(1. / 1.4);
//            }
//
//            private void zoom(double d) {
//
//                group.scaleXProperty().set(group.scaleXProperty().get() * d);
//                group.scaleYProperty().set(group.scaleYProperty().get() * d);
//            }
//
//        });
//        vbox.getChildren().add(minus);

        this.setOnScroll(new EventHandler<ScrollEvent>() {
            @Override
            public void handle(ScrollEvent event) {
                dragBaseX = group.translateXProperty().get();
                dragBaseY = group.translateYProperty().get();
                if (event.getDeltaY() > 0) {
                    group.setTranslateX(group.translateXProperty().get() * 1.4);
                    group.setTranslateY(group.translateYProperty().get() * 1.4);
                    zoom(1.4);
                } else {
                    group.setTranslateX(group.translateXProperty().get() * (1. / 1.4));
                    group.setTranslateY(group.translateYProperty().get() * (1. / 1.4));
                    zoom(1. / 1.4);
                }
            }

            private void zoom(double d) {
                group.scaleXProperty().set(group.scaleXProperty().get() * d);
                group.scaleYProperty().set(group.scaleYProperty().get() * d);
            }
        });

//        final Button col2 = new Button("color1");
//        col2.setOnAction(new EventHandler<ActionEvent>() {
//            public void handle(ActionEvent event) {
//
//                for (MCMapDataObject o : MCCurrentState.getInstance().getCurrentMap().getData().getObjectsMap().values()) {
//                    if (o.getObjectValue() > 10) {
//                        for (Path p : o.getPathList()) {
//                            p.setFill(Color.GREEN);
//                        }
//                    }
//                }
//            }
//        });
//        vbox.getChildren().add(col2);
//        final Button col1 = new Button("color2");
//        col1.setOnAction(new EventHandler<ActionEvent>() {
//            public void handle(ActionEvent event) {
//
//                for (MCMapDataObject o : MCCurrentState.getInstance().getCurrentMap().getData().getObjectsMap().values()) {
//                    for (Path p : o.getPathList()) {
//                        p.setFill(Color.YELLOW);
//                    }
//                }
//            }
//        });
//        vbox.getChildren().add(col1);
//        this.getChildren().add(vbox);
    }


    private static class MCHolder {
        private static final MCWorkspacePane INSTANCE = new MCWorkspacePane();
    }
}
